package com.xm.servlet.background.commodity;

import com.google.gson.Gson;
import com.xm.entity.Commodity;
import com.xm.entity.CommoditySpecification;
import com.xm.service.background.CommodityServeService;
import com.xm.service.background.CommodityService;
import com.xm.service.background.CommoditySpecificationService;
import com.xm.service.background.CommodityWarehouseService;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 新增商品
 * @author 康文程
 * @date 2024-1-20 下午 05:34
 */
@WebServlet("/background/add/commodity")
public class AddCommodityServlet extends HttpServlet {
    /**
     * 商品Service
     */
    private final CommodityService service = new CommodityService();
    /**
     * 商品规格Service
     */
    private final CommoditySpecificationService commoditySpecificationService = new CommoditySpecificationService();
    /**
     * 商品服务Service
     */
    private final CommodityServeService commodityServeService = new CommodityServeService();
    /**
     * 商品仓库
     */
    private final CommodityWarehouseService commodityWarehouseService = new CommodityWarehouseService();

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
        Integer commodityId =
                service.addCommodity(new Gson().fromJson(req.getParameter("commodityJson"), Commodity.class));
        System.out.println(commodityId);
        CommoditySpecification commoditySpecification = new Gson().fromJson(req.getParameter("commoditySpecificationJson"), CommoditySpecification.class);
        commoditySpecification.setCommodityId(commodityId);
        commoditySpecificationService.insertCommoditySpecification(commoditySpecification);
        commodityWarehouseService.addCommodityWarehouse(10,commodityId);
        commodityWarehouseService.addCommodityWarehouse(11,commodityId);
        commodityWarehouseService.addCommodityWarehouse(12,commodityId);
        commodityWarehouseService.addCommodityWarehouse(13,commodityId);
        commodityWarehouseService.addCommodityWarehouse(14,commodityId);
        commodityWarehouseService.addCommodityWarehouse(16,commodityId);
        commodityWarehouseService.addCommodityWarehouse(17,commodityId);
        resp.getWriter().print(commodityServeService.addCommodityServe(commodityId));
    }
}
